package com.ruoyi.safe.controller.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safe.domain.BizEducate;
import com.ruoyi.safe.domain.dto.BizEducateAppDTO;
import com.ruoyi.safe.domain.dto.EntityIdAppDTO;
import com.ruoyi.safe.domain.dto.EntityIdsAppDTO;
import com.ruoyi.safe.service.IBizEducateService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班前教育（app端）----->张鹏
 */
@RestController
@RequestMapping("/api/educate")
public class EducateController extends BaseController {
    @Autowired
    IBizEducateService bizEducateService;
    @Autowired
    ISysUserService sysUserService;

    /**
     * 新增班前教育
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizEducateAppDTO bizEducateAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(bizEducateAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", bizEducateAppDTO.getUserName()));
        }
        BizEducate bizEducate = new BizEducate();
        BeanUtils.copyProperties(bizEducateAppDTO,bizEducate);
        bizEducate.setCreateBy(sysUser.getUserName());
        bizEducate.setCreateTime(DateUtils.getNowDate());
        return toAjax(bizEducateService.insertBizEducateNoLogin(bizEducate));
    }

    /**
     * 获取班前教育详细信息
     */
    @PostMapping(value = "/info")
    public AjaxResult getInfo(@Validated @RequestBody EntityIdAppDTO entityIdAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(entityIdAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", entityIdAppDTO.getUserName()));
        }
        return success(bizEducateService.selectBizEducateById(entityIdAppDTO.getId()));
    }
    /**
     * 查询班前教育列表
     */
    @PostMapping("/list")
    public R<Object> list(@RequestBody  BizEducateAppDTO bizEducateAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(bizEducateAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", bizEducateAppDTO.getUserName()));
        }
        PageUtils.startPage(bizEducateAppDTO.getPageNum(),bizEducateAppDTO.getPageSize());
        PageUtils.orderBy("educate_time desc");
        BizEducate bizEducate = new BizEducate();
        BeanUtils.copyProperties(bizEducateAppDTO,bizEducate);
        if(bizEducateAppDTO.isMyself()){
            //只查询自己发起的班前教育
            bizEducate.setCreateBy(sysUser.getUserName());
        }
        List<BizEducate> bizEducates = bizEducateService.selectBizEducateListForApp(bizEducate,sysUser.getUserName());
        return  R.ok(new PageInfo<>(bizEducates));
    }

    /**
     * 修改班前教育
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody BizEducateAppDTO bizEducateAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(bizEducateAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", bizEducateAppDTO.getUserName()));
        }
        BizEducate bizEducate = new BizEducate();
        BeanUtils.copyProperties(bizEducateAppDTO,bizEducate);
        bizEducate.setUpdateBy(sysUser.getUserName());
        bizEducate.setUpdateTime(DateUtils.getNowDate());
        return toAjax(bizEducateService.updateBizEducateNoLogin(bizEducate));
    }

    /**
     * 删除班前教育
     */
    @PostMapping("/delete")
    public AjaxResult remove(@Validated @RequestBody EntityIdAppDTO entityIdAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(entityIdAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", entityIdAppDTO.getUserName()));
        }
        return toAjax(bizEducateService.deleteBizEducateById(entityIdAppDTO.getId()));
    }

    /**
     * 批量删除班前教育
     */
    @PostMapping("/deleteByIds")
    public AjaxResult deleteByIds(@Validated @RequestBody EntityIdsAppDTO entityIdsAppDTO) {
        SysUser sysUser = sysUserService.selectUserByUserName(entityIdsAppDTO.getUserName());
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", entityIdsAppDTO.getUserName()));
        }
        return toAjax(bizEducateService.deleteBizEducateByIds(entityIdsAppDTO.getIds()));
    }
}
